﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class pages_Default : System.Web.UI.Page
{
    public static string strType;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Label1.Text =DB.TYPE_SECTION;
            bind();//调用bind方法绑定LIstBox
        }
    }
    public void bind()
    {
        //根据全局变量TYPE_SECTION的值从tb_BigType表中筛选记录
        DataSet ds = DB.reDs("select * from tb_BigType where Type= '" + DB.TYPE_SECTION + "'");
        this.ListBox1.DataSource = ds;
        //将数据绑定到ListBox1上
        ListBox1.DataSource = ds;
        ListBox1.DataTextField = "Title";
        ListBox1.DataValueField = "ID";
        ListBox1.DataBind();
        ListBox1.SelectedIndex = -1;
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        int sequence = 0;
        //以下三行代码是自动生成 编码序号(Sequence)
        DataSet ds1=DB.reDs("SELECT Max(Sequence) FROM tb_BigType WHERE Type='" + DB.TYPE_SECTION + "'");
        string strSequence = ds1.Tables[0].Rows[0][0].ToString();
        if (strSequence == "")
        {
            if(DB.TYPE_SECTION =="部门")
                sequence = 11;
            if (DB.TYPE_SECTION == "员工")
                sequence = 21;
            if (DB.TYPE_SECTION == "仓库")
                sequence = 31;
            if (DB.TYPE_SECTION == "计量单位")
                sequence = 41;
            if (DB.TYPE_SECTION == "入库类型")
                sequence = 51;
            if (DB.TYPE_SECTION == "物资大类")
                sequence = 61;
            if (DB.TYPE_SECTION == "收付款方式")
                sequence = 71;
            if (DB.TYPE_SECTION == "出库类型")
                sequence = 81;

        }
        else
        {
            sequence = Convert.ToInt32(strSequence.Substring(0, 1) + Convert.ToString(Convert.ToInt32(strSequence.Substring(1, strSequence.Length - 1)) + 1));
        }
        string cmdText = "INSERT tb_BigType(Sequence,Title,Type) VALUES('" + sequence + "','" + tbxNameValue.Text.Trim() + "','" + DB.TYPE_SECTION + "')";
        DB.ExSql(cmdText);
        bind();//重新绑定ListBox
        this.tbxNameValue.Text = "";
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.SelectedValue != "")
        {
            //取得ListBox中选定的记录的主键
            string cmdText = "DELETE FROM tb_BigType WHERE ID='" + this.ListBox1.SelectedValue + "'";
            DB.ExSql(cmdText);
            DataTable dt = DB.ExcuteDataTable("select Title from tb_Type where Type='" + this.ListBox1.SelectedItem.Text + "'");
            foreach (DataRow dr in dt.Rows)
            {
                cmdText = "DELETE FROM tb_Material WHERE WZType='" + dr[0].ToString() + "'";
                DB.ExSql(cmdText);
            }
            cmdText = "DELETE FROM tb_Type WHERE Type='" + this.ListBox1.SelectedItem.Text + "'";
            DB.ExSql(cmdText);
            bind();//重新绑定ListBox
        }
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DB.TYPE_SECTION == "物资大类")
        {
            DataTable dt = DB.ExcuteDataTable("select count(*) from tb_Type where Type='" + this.ListBox1.SelectedItem.Text + "'");
            string countType = dt.Rows[0][0].ToString();
            if (countType != "0")
            {
                strType = "你删除的类别中包含物资类别，如删除此项，则将删除该物资大类的物资类别及物资信息。";
            }
            else
            {
                strType = "";
            }
            strType += "你确认要删除此选项吗？";
        }
        else
        {
            strType= "你确认要删除此选项吗？";            
        }

        //实现单击【删除】按钮时，弹出警告信息
        btnDelete.Attributes["onclick"] = "javascript:return confirm('" + strType + "')";
       
    }
}
